// TODO: clean code.
var tout=setTimeout(stopProgram,15000);
var continueLoading=true;
var startOk=false;
var ocanvas;


function gobj(id)
{
	return document.getElementById(id);
}
function setText(text)
{
	gobj("divStatus").innerHTML=text;
}

function main()
{
  //setText("Wait...");
  ok=false;
	try
	{
		ocanvas = new JSCanvasLib("canvasDemo");
    
		
    if(ocanvas.canWork==true)
    {
  		/*
  		setText("Version: "+ocanvas.getVersion());
  		ocanvas.setColor("#000000");
  		ocanvas.drawArc(160,100,50,90,45,false);
  		ocanvas.drawLine(0,0,100,100);
  		ocanvas.setFont("12pt Arial");
  		ocanvas.drawText(40,40,"#00ff00","Hello world!!");
  		ocanvas.setFont("12pt Serif");
  		ocanvas.drawText(40,100,"#000000","Hello world!!");
  		*/
  		ocanvas.setFont("10pt Serif");
  		ok=true;
  		imgs=new Array();
  		
  		for(i = 1;i<52;i++)
  		{
  			// Note: This images are for testing the loading animation.
  			imgs["img"+i]="http://www.improvedsource.com/files/solitaire/images/"+i+".jpg"
  		}
  		
  		ocanvas.loadImages(imgs,loading);
    }else{
      fatalError("[" + ocanvas.lastError + "]");
    }
		
	}catch(err)
	{
		alert(err.message);
	}
}
function loading(ocanvas,imgname,per1,errors,lst)
{
	xpos=0;
	ypos=0;
	if(continueLoading==true)
	{
		ocanvas.fillRect(0,0,320,200,"white","white");
		ocanvas.fillRect(xpos,ypos,100,20,"red","black");
		ocanvas.fillRect(xpos,ypos,100*per,20,"green","blue");
		per100=per1*100;
		ocanvas.drawText(xpos+5,ypos+12,"black","Loading: " + per100.toFixed(2));
		//ocanvas.drawText(xpos,ypos+40,"black","Image: " + imgname);
		if(per1==1)
		{
			start(ocanvas,lst);
		}
	}
}
function fatalError(text)
{
	continueLoading=false;
  clearTimeout(tout);
  setText("Fatal error: " + text)
}
function stopProgram()
{
	if(startOk==false)
	{
		continueLoading=false;
		ocanvas.fillRect(0,0,320,200,"white","white");
		ocanvas.drawText(0,0,"black","Loading timeout.");
	
		setText("Loading timeout.");	
	}
	setText("Loading timeout");
}
function start(ocanvas,lst)
{
	clearTimeout(tout);
	continueLoading=false;
	startOk=true;
	ocanvas.fillRect(0,0,320,200,"green","green");
	ocanvas.drawText(100,100,"black","ok");
	x=0;
	y=0;
	lst["img2"].draw(10,10);
	for(e in lst)
	{
		x+=5;
		//lst[e].draw(x,y);
	}
}


